// pages/server_end/index/index.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  shoplist:[],
    list1: [{
      name: '全部',
      dot: false,
      number: 0,
      checkinfo: 0
    }, {
      name: '待接单',
      dot: true,
      number: 0,
      checkinfo: 1,
    },
	// {
 //      name: '今日服务',
 //      dot: true,
 //      number: 0,
 //      checkinfo: 2
 //    }, 
	{
      name: '待服务',
      dot: true,
      number: 0,
      checkinfo: 3
    }, {
      name: '服务中',
      dot: false,
      number: 0,
      checkinfo: 4
    },
	{
      name: '等待验收',
      dot: false,
      number: 0,
      checkinfo: 6
    },
	{
      name: '已完成',
      dot: false,
      checkinfo: 5
    }],
    nindex: 0,
    page: 1,
    list: [],
    checkinfo: 0,
    show: false,
    cost: 0,
    nodata: 1,
    keyword: '',
    showmoney: false,
    orderId: 0,
    noOrder: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token: res.token
          })
		   
          that.getWorkerInfo();
          that.getOrdernum();
          that.getOrder();
		
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  gomessage() {
    wx.navigateTo({
      url: '../message/message'
    })
  },
  goedit() {
    wx.navigateTo({
      url: '../editinfo/editinfo'
    })
  },
  getWorkerInfo: function () {
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.workerinfo, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (res.data.code == 1) {
        that.setData({
          userinfo: res.data.data
        })
      }
    })
  },
  getOrdernum: function () {
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.workernumorder, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (res.data.code == 1) {
        var sitem = 'list1[0].number';
        that.setData({
          [sitem]: res.data.data.checkinfo
        })
        var sitem = 'list1[1].number';
        that.setData({
          [sitem]: res.data.data.checkinfo1
        })
        var sitem = 'list1[2].number';
        that.setData({
          [sitem]: res.data.data.checkinfo2
        })
        var sitem = 'list1[3].number';
        that.setData({
          [sitem]: res.data.data.checkinfo3
        })
        var sitem = 'list1[4].number';
        that.setData({
          [sitem]: res.data.data.checkinfo4
        })
        var sitem = 'list1[5].number';
        that.setData({
          [sitem]: res.data.data.checkinfo6
        })
        var sitem = 'list1[6].number';
        that.setData({
          [sitem]: res.data.data.checkinfo5
        })
      }
    })
  },
  getOrder: function () {
    var that = this;
    let param = {
      keyword: that.data.keyword,
      checkinfo: that.data.checkinfo,
      page: that.data.page,
      token: that.data.token,
	  order_type:'20'
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.workerorderlist, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (that.data.page == 1) {
        that.setData({
          list: [],
          noOrder: 1,
        })
      }
      var contentlistTem = that.data.list;
      if (res.data.data.data.length > 0) {
        var contentlist = res.data.data.data;
        if (contentlist.length < 10) {
          that.setData({
            list: contentlistTem.concat(contentlist),
            hasMoreData: false,
            noOrder: 0,
          })
        } else {
          that.setData({
            list: contentlistTem.concat(contentlist),
            hasMoreData: true,
            page: that.data.page + 1,
            noOrder: 0,
          })
        }
      } else {
        that.setData({
          hasMoreData: false
        })
      }
    })

  },
  goDetail(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../order_expert/order_expert?id=' + id
    })
  },
  confirmOrder(id) {
    ///var id = e.currentTarget.dataset.id;
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token,
      id: id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.confirmorder, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon: "none"
      })
      if (res.data.code == 1) {
        that.setData({
          page: 1,
        })
        that.getOrdernum();
        that.getOrder();
      }
    })
  },
  refuseOrder(id) {
    ///var id = e.currentTarget.dataset.id;
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token,
      id: id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.workerrefuseorder, param, url.REQUEST_METHOD_POST).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon: "none"
      })
      if (res.data.code == 1) {
        that.setData({
          page: 1,
        })
        that.getOrdernum();
        that.getOrder();
      }
    })
  },
  finishOrder(e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token,
      id: id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.finishorder, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon: "none"
      })
      if (res.data.code == 1) {
        that.setData({
          page: 1,
        })
        that.getOrdernum();
        that.getOrder();
      }
    })
  },
  cancelrefund(e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token,
      id: id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.cancelrefund, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon: "none"
      })
      if (res.data.code == 1) {
        that.setData({
          page: 1,
        })
        that.getOrdernum();
        that.getOrder();
      }
    })
  },
  startOrder(e) {
    var id = e.currentTarget.dataset.id;
	console.log(id);
	// /pages/server_end/server_ends/server_ends
	wx.navigateTo({
	  url: '/pages/server_end/server_start/server_start?id='+id
	})
    // var that = this;
    // let param = {
    //   scoure: 'wxapp',
    //   token: that.data.token,
    //   id: id
    // }
    // wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    // request.request(url.startorder, param, url.REQUEST_METHOD_GET).then((res) => {
    //   wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
    //   wx.showToast({
    //     title: res.data.msg,
    //     icon: "none"
    //   })
    //   if (res.data.code == 1) {
    //     that.setData({
    //       page: 1,
    //     })
    //     that.getOrder();
    //     that.getOrdernum();
    //   }
    // })
  },
  inputMoney() {
    var that = this;
    if (that.data.cost == 0) {
      wx.showToast({
        title: '请录入大于0的金额',
        icon: 'none'
      })
      return false;
    }
    that.close();
    let param = {
      scoure: 'wxapp',
      token: that.data.token,
      id: that.data.orderId,
      cost: that.data.cost
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.inputorder, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon: "none",
        duration: 3000
      })
      if (res.data.code == 1) {
        that.setData({
          page: 1
        })
        setTimeout(function () {
          that.getOrder();
        }, 1500)
      }
    })
  },
  changeWorkStatus(e) {
    var status = e.currentTarget.dataset.status;
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token,
      work_status: status
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.setworkstatus, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (res.data.code == 1) {
        var sitem = "userinfo.work_status";
        that.setData({
          [sitem]: status
        })
      }
    })
  },
  changecost(e) {
    var cost = e.detail.value;
    this.setData({
      cost: cost
    })
  },
  chooseNav(e) {
    this.setData({
      nindex: e.currentTarget.dataset.current,
      checkinfo: e.currentTarget.dataset.checkinfo,
      page: 1
    })
    this.getOrder();
  },
  changekey(e) {
    var key = e.detail.value;
    this.setData({
      keyword: key
    })
  },
  makecall(e) {
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  chosePosition(e) {
    console.log(e);
    var lat = e.currentTarget.dataset.lat;
    var lng = e.currentTarget.dataset.lng;
    console.log(lat);
    wx.openLocation({
      latitude: Number(lat),
      longitude: Number(lng)
    })
  },
  gotoorder(e)
  {
  	   var id = e.currentTarget.dataset.id;
  	  wx.navigateTo({
  	    url: '../order_expert/order_expert?id='+id,
  	  })
  },
  subStart(e) {
    var that = this;
    that.setData({
      orderId: e.currentTarget.dataset.id
    })

    wx.requestSubscribeMessage({
      tmplIds: [
        '4Lrgf9Bnb2GS_vPaq-x-o0tefhfyUDZU7h6vG4y4x6Q',
        '8AunnNXQefxgIuTYEgv5tcx3uvVrMJeNBkJp7CSUtAc',
      ],
      success: (res) => {
        that.confirmOrder(that.data.orderId);
      },
      fail: (res) => {
        that.confirmOrder(that.data.orderId);
      },
    })
    //that.confirmOrder(that.orderId);
  },
  subRefuse(e) {
    var that = this;
    that.setData({
      orderId: e.currentTarget.dataset.id
    })

    that.refuseOrder(that.data.orderId);
  },

  showMoney(e) {
    this.setData({
      orderId: e.currentTarget.dataset.id,
      showmoney: true
    })
  },
  close() {
    this.setData({
      show: false,
      showmoney: false
    })
  },
  goCheckOrder(e) {
    var id = e.currentTarget.dataset.id;
	var phone = e.currentTarget.dataset.phone;
	console.log('phone:'+phone);
    wx.navigateTo({
      url: '../order_check/order_check?id=' + id+'&phone='+phone
    })
  },
 
})